﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout_lay_admin.cshtml";
}
<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
        height: 34px;
        line-height: 34px;
        padding: 0 8px;
    }

    .layui-table-view {
    }

    .divborder {
        border-top: solid 1px #27AAA5;
        border-left: solid 1px #27AAA5;
        border-right: solid 1px #27AAA5;
    }

    .layui-btn-container .layui-btn {
        margin-bottom: 0px !important;
    }
</style>


<div class="layuimini-container">
    <div class="layuimini-main">
        @*<div class="layui-btn-group" style="padding-bottom:10px;">
                <button class="layui-btn" id="btn-add">添加</button>
            </div>*@
        <div class="layui-form layuimini-form">
            <div style="margin: 10px 10px 10px 10px">

                <div class="layui-inline ">
                    <label class="layui-form-label">@ViewBag.KeyValueName</label>
                    <div class="layui-input-inline">
                        <input type="text" name="keyword" id="keyword" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button id="btn-search" class="layui-btn " lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i>@ViewBag.Search</button>
                </div>

            </div>
        </div>
        <div>
            @*<div class="layui-btn-group">
                    <button class="layui-btn" id="btn-expand">全部展开</button>
                    <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                    <button class="layui-btn " id="btn-fresh">刷新</button>
                </div>*@
            @*<div class="layui-btn-container divborder" style=" padding-top:4px ; padding-bottom:4px ; padding-left:10px;">
                    <button class="layui-btn layui-btn-sm data-add-btn" id="btn-add"> @ViewBag.Add </button>
                </div>*@
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>

            <!-- 操作列 -->
            <script type="text/html" name="auth-state" >
                <a ex_btn="ex_add" class="layui-btn layui-btn-xs layui-btn-x" lay-event="add"> 添加下级 </a>
                <a ex_btn="ex_edit" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit"> @ViewBag.Edit</a>
                <a ex_btn="ex_delete" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"> @ViewBag.Delete</a>
            </script>
        </div>


    </div>

</div>

<script src="/Content/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script>
    var treetable;
    var reloadTable = function () {
        var keyword = $("#keyword").val();
        var _url = '/Base_KeyValue/Search' + "?keyword=" + keyword;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: '00000000-0000-0000-0000-000000000000',
            treeIdName: 'KeyValue_Id',
            treePidName: 'KeyValue_ParentId',
            elem: '#munu-table',
            url: _url,
            page: false,
            cols: [[
                { type: 'numbers' },
                { field: 'KeyValue_Name', minWidth: 200, title: '@ViewBag.KeyValueName' },
                //{ field: 'authority', title: '权限标识' },
                //{ field: 'menuUrl', title: '菜单url' },
                { field: 'KeyValue_Sort', width: 80, align: 'center', title: '排序号' },
                {
                    field: 'KeyValue_Level', width: 180, align: 'center', templet: function (d) {
                        var arrbutton = listButton_Code.split(',');
                       
                        if (d.KeyValue_Code.length == 3) {
                            var str = '';
                            for (var i = 0; i < arrbutton.length; i++) {
                                if (arrbutton[i] == "ex_add") {
                                    str = '<a class="layui-btn layui-btn-xs layui-btn-x" lay-event="add">' + button_txt.addchild + '</a>';
                                }
                            }
                            return str;
                        }
                        if (d.KeyValue_Code.length == 6) {
                            var str = '';
                            for (var i = 0; i < arrbutton.length; i++) {
                                if (arrbutton[i] == "ex_edit") {
                                    str += '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">'+button_txt.edit+'</a>';
                                } else if(arrbutton[i] == "ex_delete") {
                                    str += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">'+button_txt.delete+'</a> ';
                                }
                            }
                            return str;                           
                        } else {
                            return '';
                        }
                    }, title: '操作'
                },
                //{ templet: '#auth-state', width: 220, align: 'center', title: '操作' }
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });
    }

    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        window.treetable = layui.treetable;

        reloadTable();

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        $("#btn-fresh").click(function () {
            reloadTable();
        });

        $("#btn-search").click(function () {
            reloadTable();
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('确定要禁用选中的账户吗？', function () {

                    $.ajax({
                        url: '/Base_KeyValue/ActivateSwich',
                        type: 'post',
                        dataType: 'text',
                        data: { "Ids": data.id },
                        success: function (resp) {
                            var json = eval('(' + resp + ')');
                            layer.alert(json.Message);
                            reloadTable();
                        },
                        error: function (resp) {
                            layer.alert("对不起出错了");
                        }
                    })
                })
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '修改组织',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['600px', '400px'],
                    content: '/Base_KeyValue/Edit?primarykey=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });


            } else if (layEvent === 'add') {

                var index = layer.open({
                    title: '添加组织',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['600px', '400px'],
                    content: '/Base_KeyValue/Create?parentid=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

            }
        });

        $("#btn-add").click(function () {
              var index = layer.open({
                    title: '添加新组织',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['600px', '400px'],
                    content: '/Base_KeyValue/CreateCustom',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

        });
    });
</script>
